package com.bestcem.xm.txtanls.grpc.convert;

import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.txtanls.entity.mongo.AnalysisProject;
import com.bestcem.xm.txtanls.grpc.v1.common.BaseModel;
import com.bestcem.xm.txtanls.grpc.v1.services.TextField;
import com.bestcem.xm.txtanls.grpc.v1.services.TrainMsg;
import com.bestcem.xm.txtanls.service.dto.analysis.AnalysisProjectDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 可复用的各层模型之间的转换方法
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/5/10-18:34
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ModelConvert {
    public static com.bestcem.xm.txtanls.grpc.v1.services.AnalysisProject analysisProjectDtoToInfo(AnalysisProjectDTO dto) {
        com.bestcem.xm.txtanls.grpc.v1.services.AnalysisProject.Builder builder = com.bestcem.xm.txtanls.grpc.v1.services.AnalysisProject.newBuilder();
        if (Objects.isNull(dto)) {
            return builder.build();
        }

        if (Objects.nonNull(dto.getId()) && !ObjectUtils.isEmpty(dto.getId())) {
            builder.setId(dto.getId());
        }
        if (Objects.nonNull(dto.getUserId()) && !ObjectUtils.isEmpty(dto.getUserId())) {
            builder.setUserId(dto.getUserId());
        }
        if (Objects.nonNull(dto.getCreator()) && !ObjectUtils.isEmpty(dto.getCreator())) {
            builder.setCreator(dto.getCreator());
        }
        if (Objects.nonNull(dto.getName()) && !ObjectUtils.isEmpty(dto.getName())) {
            builder.setName(dto.getName());
        }
        if (Objects.nonNull(dto.getOrgId()) && !ObjectUtils.isEmpty(dto.getOrgId())) {
            builder.setOrgId(dto.getOrgId());
        }
        if (Objects.nonNull(dto.getStatus()) && !ObjectUtils.isEmpty(dto.getStatus())) {
            builder.setStatusValue(dto.getStatus());
        }
        if (Objects.nonNull(dto.getSurveyId()) && !ObjectUtils.isEmpty(dto.getSurveyId())) {
            builder.setSurveyId(dto.getSurveyId());
        }
        if (Objects.nonNull(dto.getTableId()) && !ObjectUtils.isEmpty(dto.getTableId())) {
            builder.setTableId(dto.getTableId());
        }
        if (Objects.nonNull(dto.getTableName()) && !ObjectUtils.isEmpty(dto.getTableName())) {
            builder.setTableName(dto.getTableName());
        }
        if (Objects.nonNull(dto.getTimeFieldId()) && !ObjectUtils.isEmpty(dto.getTimeFieldId())) {
            builder.setTimeFieldId(dto.getTimeFieldId());
        }
        if (Objects.nonNull(dto.getTimeFieldName()) && !ObjectUtils.isEmpty(dto.getTimeFieldName())) {
            builder.setTimeFieldName(dto.getTimeFieldName());
        }
        if (Objects.nonNull(dto.getRecordFlag()) && !ObjectUtils.isEmpty(dto.getRecordFlag())) {
            builder.setFlagValue(dto.getRecordFlag());
        }

        if (Objects.nonNull(dto.getTextFieldList()) && !CollectionUtils.isEmpty(dto.getTextFieldList())) {
            TextField.Builder textBuilder = TextField.newBuilder();
            List<TextField> textFields = new ArrayList<>();
            for (AnalysisProject.TextField textField : dto.getTextFieldList()) {
                textBuilder.setId(textField.getId());
                textBuilder.setValue(textField.getValue());
                textFields.add(textBuilder.build());
            }
            builder.addAllTextFields(textFields);
        }

        TrainMsg.Builder msgBuilder = TrainMsg.newBuilder();
        if (Objects.nonNull(dto.getTrainStatus()) && !ObjectUtils.isEmpty(dto.getTrainStatus())) {
            msgBuilder.setStatusValue(dto.getTrainStatus());
        }
        if (Objects.nonNull(dto.getTrainRemainTime()) && !ObjectUtils.isEmpty(dto.getTrainRemainTime())) {
            msgBuilder.setTrainRemainTime(dto.getTrainRemainTime());
        }
        builder.setTrainMsg(msgBuilder);

        BaseModel.Builder baseModelBuilder = BaseModel.newBuilder();
        if (Objects.nonNull(dto.getCreatedDate()) && !ObjectUtils.isEmpty(dto.getCreatedDate())) {
            baseModelBuilder.setCreateTime(DateUtil.getTimestamp(dto.getCreatedDate()));
        }
//        if (Objects.nonNull(dto.getCreatedId()) && !ObjectUtils.isEmpty(dto.getCreatedId())) {
//            baseModelBuilder.setCreatedId(dto.getCreatedId());
//        }
        if (Objects.nonNull(dto.getUpdatedDate()) && !ObjectUtils.isEmpty(dto.getUpdatedDate())) {
            baseModelBuilder.setUpdateTime(DateUtil.getTimestamp(dto.getUpdatedDate()));
        }
        if (Objects.nonNull(dto.getUpdatedId()) && !ObjectUtils.isEmpty(dto.getUpdatedId())) {
            baseModelBuilder.setUpdatedId(dto.getUpdatedId());
        }
        builder.setBaseTime(baseModelBuilder);

        return builder.build();
    }
}
